package com.chookss.tools.okgoconfig;

import android.content.Context;

import com.aliyun.common.utils.StringUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * okgo网络请求类工具
 */
public class OkGoUtil {
    public static <T> void getRequets(String url, Object tag, Map<String, String> map, JsonCallback<T> callback) {
        KLog.i("OkGoUtil", "method get");
        OkGo.<T>get(url)
                .tag(tag)
                .params(map)
                .execute(callback);
    }

    public static <T> void postRequest(String url, Object tag, Map<String, String> map, JsonCallback<T> callback) {
        KLog.i("OkGoUtil", "method post");
        OkGo.<T>post(url)
                .tag(tag)
                .params(map)
                .execute(callback);
    }

    public static <T> void upLoadFiles(String url, Object tag, Map<String, String> map, String fileType, List<File> files, JsonCallback<T> callback) {
        KLog.i("OkGoUtil", "method post upLoadFiles");
        OkGo.<T>post(url)
                .tag(tag)
                .params(map)
                .addFileParams(fileType, files)           // 这种方式为同一个key，上传多个文件
                .execute(callback);
    }

    /**
     * 网络返回错误时候
     *
     * @param response
     */
    public static void onErrorTips(Context context, Response response) {
        if(null==context||null==response)return;
        String message = response.getException().getMessage();
        if (StringUtils.isEmpty(message)) return;
        try {
            if (message.contains("Failed to")||message.contains("resolve host"))
                MyToast.t(context, "网络异常");
            else if (message.contains("BEGIN_OBJECT")||message.contains("IllegalStateException")||message.contains("4") || message.contains("5")||message.contains("failed"))
                MyToast.t(context, "服务器异常，请稍后重试");
            else if(message.contains("but"))
                MyToast.t(context, "数据格式不正确");
            else
                MyToast.t(context, message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 网络返回错误时候 @param String
     */
    public static void onErrorTips(String message) {

        if (StringUtils.isEmpty(message)) return;
        if (message.contains("Failed to") || message.contains("resolve host")) {
        } else if (message.contains("BEGIN_OBJECT") || message.contains("IllegalStateException") || message.contains("4") || message.contains("5") || message.contains("failed")) {
        } else if (message.contains("but")) {
        } else  MyToast.t(message);
    }
    /**
     * 网络返回错误时候 @param String
     */
    public static void onErrorIntent(String code,String message) {
        if(Utils.isNull(code)){
            return;
        }
        if("400".equals(code)){
            MyToast.show(message);
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("code", code);
        map.put("msg", message);
        EventBus.getDefault().post(new MyEvent(map));
    }
}